package com.starblink.login.feedback.ui

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.util.IntentUtils
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.basic.util.CommUtils
import com.starblink.login.databinding.ActivityFeedbackBinding

@Route(path = RoutePage.Login.FEEDBACK)
class FeedbackActivity : BaseTVMActivity<ActivityFeedbackBinding, FeedbackVM>() {

    override fun initViewBinding(): ActivityFeedbackBinding =
        ActivityFeedbackBinding.inflate(layoutInflater)

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //返回按钮
        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })
        //checkbox
        viewBinding.feedbackCheck.click {
            if (viewModel.isAgree) {
                viewBinding.feedbackCheck.setImageResource(com.starblink.basic.style.R.drawable.ic_checkbox_false_black)
            } else {
                viewBinding.feedbackCheck.setImageResource(com.starblink.basic.style.R.drawable.ic_checkbox_true_black)
            }
            viewModel.isAgree = !viewModel.isAgree
            //刷按钮
            viewModel.checkConfirm()
        }
        //输入监听
        viewBinding.txtInput.addTextChangedListener(onTextChanged = { text, _, _, _
            ->
            run {
                viewModel.txtStr = text.toString()
                viewModel.checkConfirm()
                viewBinding.txtInputMax.text = "${viewModel.txtStr.length}/500"
            }

        })
        //按钮监听变色
        viewModel.confirm.observe(this) {
            viewBinding.fdBtn.isEnabled = it
        }

        //按钮点击
        viewBinding.fdBtn.click {
            if (viewModel.isAgree) {
                IntentUtils.sendMailOnlyText(
                    viewModel.txtStr,
                    CommUtils.getString(com.starblink.basic.style.R.string.feedback_title)
                )
            } else {
                if (viewModel.txtStr.isBlank()) {
                    toast(CommUtils.getString(com.starblink.basic.style.R.string.feedback_toast1))
                } else if (viewModel.txtStr.length > 500) {
                    toast(CommUtils.getString(com.starblink.basic.style.R.string.feedback_toast2))
                } else {
                    viewModel.confirmUpload()
                }
            }
        }
    }
}
